/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jdemonbd.DAO.Sqlite;

import java.sql.*;
import jdemonbd.DAO.iDAO;



/**
 *
 * @author kakashi
 */
public class daoSQLite implements iDAO{

    private Connection conn;
    private String bd;
    private String error;
    private String msg;
    private ResultSet rs;
    private Statement st;
    private String sql;

    public String getConsultaSql() {
        return sql;
    }

    public void setConsultaSql(String sql) {
        this.sql = sql;
    }

    
    public daoSQLite()
    {
        this.conn = null;
    }
    public daoSQLite(String bd)
    {
        this.bd = bd;
        this.conn = null;
    }
    
    public Connection conectar() {
        try {
            Class.forName("org.sqlite.JDBC");
            conn = DriverManager.getConnection("jdbc:sqlite:"+this.getBd());
            this.st = conn.createStatement();
        } catch (Exception e) {
            this.conn = null;
            //JOptionPane.showMessageDialog(null, "Error al conectar con la base de datos","Error",JOptionPane.ERROR);
            this.error = "Error al conectar con la base de datos";
        }
        return conn;
    }

    public void cerrar() {
        try {
            this.conn.close();
        } catch (SQLException ex) {
             this.conn = null;
            //JOptionPane.showMessageDialog(null, "Error al cerrar la base de datos","Error",JOptionPane.ERROR);
             this.error = "Error al cerrar la conexion con la base de datoss";
        }
    }


    /*public ResultSet seleccionar(String sql)
    {
        if(this.st != null)
        {
            try {
                this.rs = this.st.executeQuery(sql);
            } catch (SQLException ex) {
                this.rs = null;
            }
        }
        return this.rs;
    }
*/
    public ResultSet consultar(String sql)
    {
        if(this.st != null)
        {
            try {
                this.rs = this.st.executeQuery(sql);
            } catch (SQLException ex) {
                this.rs = null;
            }
        }
        return this.rs;
    }
    
    public ResultSet seleccionar(String tabla)
    {
        sql = "SELECT * FROM " + tabla + ";";
        //JOptionPane.showMessageDialog(null, sql);
        if(this.st != null)
        {
            try {
                this.rs = this.st.executeQuery(sql);
            } catch (SQLException ex) {
                this.rs = null;
            }
        }
        return this.rs;
    }
    
    
    public ResultSet seleccionar(String tabla, String condicion)
    {
         sql = "SELECT * FROM " + tabla + " WHERE " + condicion +";";
        if(this.st != null)
        {
            try {
                this.rs = this.st.executeQuery(sql);
            } catch (SQLException ex) {
                this.rs = null;
            }
        }
        return this.rs;
    }
    
    //// SELECT COUNT(*) FROM TABLA

    public int seleccionarContar(String tabla)
    {
        int cant = 0;
        sql = "SELECT COUNT(*) as CONT FROM " + tabla + ";";
        //JOptionPane.showMessageDialog(null, sql);
        if(this.st != null)
        {
            try {
                this.rs = this.st.executeQuery(sql);
                rs.next();
                cant = rs.getInt("CONT");
            } catch (SQLException ex) {
                this.rs = null;
            }
        }
        return cant;
    }

    public int seleccionarContar(String tabla, String condicion)
    {
        int cant = 0;
        sql = "SELECT COUNT(*) as CONT FROM " + tabla + " WHERE " + condicion +";";
        if(this.st != null)
        {
            try {
                this.rs = this.st.executeQuery(sql);
                rs.next();
                cant = rs.getInt("CONT");
            } catch (SQLException ex) {
                this.rs = null;
            }
        }
        return cant;
    }
    

        
    public void insertar(String tabla)
    {
        
    }

/////////////////////////////////////////

    public Connection getConn() {
        return conn;
    }

    public String getError() {
        return error;
    }

    public String getMsg() {
        return msg;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public String getBd() {
        return bd;
    }

    public void setBd(String bd) {
        this.bd = bd;
    }

    public Connection getConexion() {
        return this.conn;
    }
    

}
